package com.commerce.addressbook.activity;

import androidx.annotation.RequiresPermission;
import androidx.appcompat.app.AppCompatActivity;
import androidx.exifinterface.media.ExifInterface;


import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.commerce.addressbook.Entity.Contacts;
import com.commerce.addressbook.GlobalVariable;
import com.commerce.addressbook.R;
import com.github.cropbitmap.LikeQQCropView;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CropActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);
        final LikeQQCropView likeView=findViewById(R.id.likeView);
        Button button=findViewById(R.id.btClip);
        Bitmap bitmap = null;
        Intent intent = getIntent();
        InputStream is = null;
        int rotate = getOrientationRotate(intent.getData());
        try {
            is = getContentResolver().openInputStream(intent.getData());
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bitmap = rotateBitmapByDegree(bitmap, rotate);
        //如果传入的bitmap过大,此方法有OOM的可能
        likeView.setBitmap(bitmap);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**裁剪*/
                Bitmap clip = likeView.clip();
                try {
                    String path = GlobalVariable.mTempPhotoPath();
                    //存图片
                    FileOutputStream fos = new FileOutputStream(path);
                    fos.write(bitmap2Bytes(clip, Bitmap.CompressFormat.JPEG));
                    fos.close();
                    Contacts contacts = GlobalVariable.getGlobalVariable().getContacts();
                    contacts.setImgUrl(path);
                    int update = contacts.update(contacts.getId());
                   finish();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

    }
    public static byte[] bitmap2Bytes(final Bitmap bitmap, final Bitmap.CompressFormat format) {
        if (bitmap == null) return null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(format, 100, baos);
        return baos.toByteArray();
    }

    /**
     * 获取照片属性中的旋转角度
     *
     * @param data 图片的uri
     * @return 照片属性中的旋转角度
     */
    public int getOrientationRotate(Uri data) {
        int degree = 0;
        try {
            InputStream is = getContentResolver().openInputStream(data);
            // 从指定路径下读取图片，并获取其Exif信息
            ExifInterface exifInterface = new ExifInterface(is);
            // 获取图片的旋转信息
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }
    /**
     * 根据角度值旋转Bitmap
     *
     * @param bitmap
     * @param degree
     * @return
     */
    private static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {

        // 根据旋转角度，得到旋转矩阵
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);

        // 将原始图片按照旋转矩阵进行旋转，得到新的图片
        Bitmap result = null;
        try {
            result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            result = bitmap;
        }

        if (bitmap != result) {
            bitmap.recycle();
        }
        return result;

    }
}